package cn.samir.sudoku.db.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.samir.sudoku.db.DBHelper;
import cn.samir.sudoku.image.SavePair;
import cn.samir.sudoku.util.BeanUtils;

public class SavePairDao {
	private DBHelper dbHepler;
	private final String table = "savepair";
	Context context;

	public SavePairDao(Context context) {
		this.context = context;
		dbHepler = new DBHelper(context);
	}

	public int insert(SavePair savePair) {
		if (savePair == null)
			return -1;
		SQLiteDatabase db = dbHepler.getWritableDatabase();
		if (db == null || !db.isOpen()) {
			return -1;
		}
		if ((query(savePair.name) != null)) {
			return 1;
		}

		return (int) db.insert(table, null, BeanUtils.BeanToValues(savePair));
	}

	public String query(String name) {

		if (name == null)
			return null;
		SQLiteDatabase db = dbHepler.getReadableDatabase();
		if (db == null || !db.isOpen()) {
			return null;
		}
		Cursor c = db.query(table, null, " name=? ", new String[] { name },
				null, null, null);
		if (c.moveToFirst()) {
			return BeanUtils.cursorToBean(c, SavePair.class).path;
		}
		return null;
	}
}
